package com.og.vas.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.vas.common.exception.Assert;
import com.og.vas.common.result.ResponseEnum;
import com.og.vas.linstener.ExcelUserListener;
import com.og.vas.pojo.dto.ExcelUserDTO;
import com.og.vas.pojo.dto.LoginDTO;
import com.og.vas.pojo.dto.RegisterDTO;
import com.og.vas.pojo.entity.UserInfo;
import com.og.vas.mapper.UserInfoMapper;
import com.og.vas.pojo.vo.LoginVO;
import com.og.vas.pojo.vo.UserInfoVO;
import com.og.vas.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.og.vas.utils.JwtUtils;
import com.og.vas.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.provider.MD5;

import java.io.InputStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzy
 * @since 2022-01-11
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Override
    public void validityCheck(String idCardNum, String phone) {
        //手机号判断用户是否被注册
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("phone", phone);
        Integer count = baseMapper.selectCount(userInfoQueryWrapper);
        Assert.isTrue(count == 0, ResponseEnum.MOBILE_EXIST_ERROR);

        //身份证判断用户是否被注册
        userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("id_card_num",idCardNum);
        count = baseMapper.selectCount(userInfoQueryWrapper);
        Assert.isTrue(count == 0, ResponseEnum.ID_EXIST_ERROR);

        //用户名判断用户是否被注册
        userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("username",idCardNum);
        count = baseMapper.selectCount(userInfoQueryWrapper);
        Assert.isTrue(count == 0, ResponseEnum.LOGIN_ERROR);
    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();

        if(loginDTO.loginWay==0){
            queryWrapper.eq("username",loginDTO.getNum());

        }else{
            queryWrapper.eq("phone",loginDTO.getNum());
        }
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);
        //断言用户存在
        Assert.notNull(userInfo, ResponseEnum.LOGIN_MOBILE_ERROR);

        //校验密码
        Assert.equals(MD5Utils.encrypt(loginDTO.getPassword()), userInfo.getPassword(), ResponseEnum.LOGIN_PASSWORD_ERROR);

        //用户状态校验
        //LOGIN_DISABLED_ERROR(-210, "用户已被禁用"),
        Assert.equals(userInfo.getStatus(), UserInfo.STAUTS_NORMAL, ResponseEnum.LOGIN_LOKED_ERROR);



        //vo生成
        LoginVO loginVO = new LoginVO();

        BeanUtils.copyProperties(userInfo,loginVO);

        //生成token

            loginVO.setToken(JwtUtils.createToken(userInfo.getId(), userInfo.getUsername(),userInfo.getIdentity()));


           return loginVO;



    }

    @Override
    public void Register(RegisterDTO registerDTO) {
        UserInfo userDO = new UserInfo();

        BeanUtils.copyProperties(registerDTO,userDO);


        //密码加密
        userDO.setPassword(MD5Utils.encrypt(registerDTO.getPassword()));


        baseMapper.insert(userDO);
    }




    @Override
    public void updatePwd(Integer userId, String oldPwd, String newPwd) {



        Assert.notEquals(oldPwd,newPwd,ResponseEnum.SAME_PASSWORD);

        UserInfo userInfo = baseMapper.selectById(userId);
        //校验密码
        Assert.equals(MD5Utils.encrypt(oldPwd), userInfo.getPassword(), ResponseEnum.LOGIN_PASSWORD_ERROR);

        userInfo.setPassword(MD5Utils.encrypt(newPwd));

        baseMapper.updateById(userInfo);


    }

    @Override
    public void updatePermission(Integer userId, Integer op) {

        Assert.isTrue(op==1||op==0,ResponseEnum.PREMISSION_SET_ERROR);

        UserInfo userInfo = baseMapper.selectById(userId);

        userInfo.setIdentity(op);

        baseMapper.updateById(userInfo);

    }

    //事务注解(异常回滚)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void imporData(InputStream inputStream) {

        EasyExcel.read(inputStream, ExcelUserDTO.class,new ExcelUserListener(baseMapper)).sheet().doRead();

    }

    @Override
    public IPage<UserInfoVO> list(Page<UserInfoVO> page) {
        return baseMapper.selectVOPage(page);
    }
}
